$(function(){
	
	
	
	// lgc 标识用户身份
	$(async function () {
	  var tips = document.getElementsByClassName("tips")[0];
	  var user = getCookie("lgc");
	  //   页面加载时 有用户名就表示用户身份  没有就去登录
	  if (user) {
	    tips.innerHTML = `欢迎您,${user} <button onclick="exit()">退出</button>`;
	  }
	  else {
	      location.href = "../html/reg.html?returnUrl=" + encodeURIComponent(location.href);
	    }
	
	  var url = urlParse();
	  var price = { num: 0 };
	
	  var { id } = url;
	
	  if (id) {
	   
	    var result = await searchGoodsByGoodsId({ ...url });
	    console.log(result);
	    var { status, msg, data } = result;
	    if (status) {
	      var {
	        id,
	        goodsid,
	        title,
	        items_txt1,
	        items_txt2,
	        items_price,
	        img,
	      } = data;
	
	      // left
	      $(".cont").html(`<div class="hd_img">
	            <img src="${img}" alt="">
	        </div>
	        <div class="hd_right">
	                            <div class="title">
	                                <span>${title}</span>
	                            </div>
	                            <div class="address">
	                                <div class="time">时间：${items_txt2}</div>
	                                <div class="place">
	                                    <div class="addr">场馆：${items_txt1}</div>
	
	                                </div>
	                            </div>
	                        </div>
	
	
	                        <!-- 预售 -->
	                        <div class="perform-notice">
	                            <div class="perform-notice-prefix">
	                                <div class="speaker"></div>
	                                <span>预售</span>
	                            </div>
	                            <div class="perform-notice-content hastitle">
	                                <div>本商品为预售商品，正式开票后将第一时间为您配票</div>
	                                <div class="perform-notice-content-detail">
	                                    预售期间，由于主办未正式开票，下单后无法立即为您配票。一般于演出前2-6周开票，待正式开票后，请您通过订单详情页或者票夹详情，查看票品信息、取票方式等演出相关信息
	                                </div>
	                            </div>
	
	                        </div>
	
	                        <!-- 场次 -->
	                        <div class="perform__order__box">
	                            <div class="notice-time"><img
	                                    src="//img.alicdn.com/tfs/TB1gKnkSMTqK1RjSZPhXXXfOFXa-28-28.png"
	                                    class="notice-time-icon">场次时间均为演出当地时间</div>
	                        </div>
	
	                        <div class="perform__order__select perform__order__select__performs">
	                            <div class="select_left">场次</div>
	                            <div class="select_right">
	                                <div class="select_right_list">
	                                    <div class="select_right_list_item active">
	                                        <span>
	                                            ${items_txt2}</span>
	
	                                        <!-- 票档 -->
	                                        <div class="perform__order__select">
	                                            <div class="select_left">票档</div>
	                                            <div class="select_right">
	                                                <div class="select_right_list">
	                                                    <div class="select_right_list_item sku_item ">
	                                                        <div class="skuname" data-price="280">
	                                                            280
	                                                        </div>
	                                                    </div>
	                                                    <div class="select_right_list_item sku_item ">
	                                                        <div class="skuname" data-price="380">
	                                                            380
	                                                        </div>
	                                                    </div>
	                                                    <div class="select_right_list_item sku_item ">
	                                                        <div class="skuname"  data-price="580">
	                                                            580
	                                                        </div>
	                                                    </div>
	                                                    <div class="select_right_list_item sku_item">
	                                                        <div class="skuname" data-price="980">
	                                                            980
	                                                        </div>
	                                                    </div>
	                                                    <div class="select_right_list_item sku_item">
	                                                        <div class="skuname" data-price="1280">
	                                                            1280
	                                                        </div>
	                                                    </div>
	                                                    <div class="select_right_list_item sku_item">
	                                                        <div class="skuname" data-price="1680">
	                                                            1680
	                                                        </div>
	                                                    </div>
	                                                </div>
	                                            </div>
	                                        </div>
	
	                                        <!-- 选票提示 -->
	                                        <div class="perform__desc__info perform__desc__info__active">
	                                            <p>按付款顺序配票，优先连座配票</p>
	                                        </div>
	                                        <div class="wrap-input">
	                                        <div class="select_left">数量：&nbsp;</div>
	                                        <div class="count">
	                                        <span class="reduce">-</span>
	                                        <input class="count-input" type="text" value="1" />
	                                        <span class="add">+</span>
	                                         </div>
	
	                                        <!-- 加购按钮 -->
	                                        <div>
	                                            <div class="buybtn">提交</div>
	                                            
	                                        </div>
	        `);
	    } else {
		
	      location.href = "../html/index.html";
	    }
	  } else {
		
	    location.href = "../html/index.html";
	  }
	
	 
	//  加号
	  $(".add").click(function () {
	
	    var num = $(this).prev().val();
	    num++;
	    console.log(num, "num");
	
	    $(this).prev().val(num);
		console.log(222)
	
	 //    var price = $(this).parent().prev().text();
		// console.log(price)
	 //    var subtotal = (price * num).toFixed(2);
		
	 //    $(this).parent().next().text(subtotal);
	 //    $(this).prevAll(".reduce").text("-");
	  });
	  
	  // 减号
	  
	
	  $(".reduce").click(function () {
	    var num = $(this).next().val();
	    if (num <= 1) return false;
	    num--;
	    if (num == 1) $(this).text("");
	    $(this).next().val(num);
	
	//     var price = $(this).parent().prev().text();
	//     var subtotal = (price * num).toFixed(2);
	//     $(this).parent().next().text(subtotal);
	  });
	
	  // 点击加入购物车
	  /*  数量 title img 价格
	    登陆的用户
	    商品的相关信息
	    买了几件
	     */
	  // 图片 标题 价格 数量 商品id
	  // 价格  获取点击的值  样式改变
	  // select_right_list的事件委托
	  var select = document.querySelector(".select_right_list");
	  // console.log(select, "select");
	  // 事件捕获addEventListener
	  select.addEventListener(
	    "click",
	    (e) => {
	      e = e || window.event;
	      var target = e.target || e.srcElement;
	      if (
	        target.className == "select_right_list_item sku_item " ||
	        target.className == "select_right_list_item sku_item" ||
	        target.className == "skuname"
	      ) {
	        // 循环 target和循环的相等 并且获取小格子的值
	        var skuname = document.querySelectorAll(".sku_item");
	        for (var i = 0; i < skuname.length; i++) {
	          skuname[i].classList.remove("active");
	        }
	        if (target.className == "skuname") {
	          target.parentElement.classList.add("active");
	          target.parentElement.classList.add("data-price");
	          price.num = target.getAttribute("data-price");
	        } else {
	          target.classList.add("active");
	          target.children[0].classList.add("data-price");
	          price.num = target.children[0].getAttribute("data-price");
	        }
	      }
	    },
	    false
	  );
	  $(document).on("click", ".buybtn", async () => {
	    // 判断当前登录的用户是否登录
	    var user = getCookie("lgc");
	    if (user) {
	      console.log(price, 'price')
	      if (!price.num) {
	        alert("请选择票档");
	        return;
	      }
	      // return
	      //如果用户已经登陆，加入购物车，存数据库
	
	      // 获取数量的值
	      var buyNum = $(".count-input").val(); //获取商品数量的值
	      // await goodslist
		  console.log(buyNum)
	
	      var goodsInfo = await searchGoodsByGoodsId({ id: url.id });
		  
	      console.log(goodsInfo, "goodsInfo");
	      var { goodsid, id, img, title } = goodsInfo.data;
	      //   把获取到的存到数据库
	      var result = await addToShoppingCar({
	        user,
	        gid: goodsid,
	        id,
	        buyNum,
	        title,
	        img,
	        priceNum: price.num,
	      }); //数据库 用户名，商品id 购买数量
	      var { status, msg } = result;
	      console.log(result);
	      if (status) {
	        if (confirm("商品购买成功,是否进入购物车?")) {
	          location.href = "../html/shopping.html";
	        }
	      } else {
	        alert(msg);
	      }
	    } else {
	      // 没有登录 => 去登录
	      // 问题
	      // 没有登录 => 去登录 => 登录成功(跳转主页 => 尴尬)
	      // 解决方法啊?
	      // 跳转登录页的同时,传递当前页的url作为 returnUrl, 登录成功再跳转回来
	      location.href =
	        "../html/reg.html?returnUrl=" + encodeURIComponent(location.href);
	    }
	  });
	});
	
	function urlParse() {
	  var search = location.search; // ?id=
	  var data = {};
	  if (search) {
	    //从注册页跳转过来  => 有数据
	    var str = search.substring(1);
	    // console.log(str);
	    var list = str.split("&");
	    // console.log(list);
	    list.forEach(function (item) {
	      var key = item.split("=")[0];
	      var val = item.split("=")[1];
	      data[key] = val;
	    });
	  }
	  return data;
	}
	
	function exit() {
	  deleteCookie("lgc");
	  location.reload();
	}
	
})